'
' *  Authors:  Benton Stark
' * 
' *  Copyright (c) 2007-2012 Starksoft, LLC (http://www.starksoft.com) 
' *
' * Permission is hereby granted, free of charge, to any person obtaining a copy
' * of this software and associated documentation files (the "Software"), to deal
' * in the Software without restriction, including without limitation the rights
' * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' * copies of the Software, and to permit persons to whom the Software is
' * furnished to do so, subject to the following conditions:
' * 
' * The above copyright notice and this permission notice shall be included in
' * all copies or substantial portions of the Software.
' * 
' * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
' * THE SOFTWARE.
' * 
' 



Namespace FTP
    ''' <summary>
    ''' FTP response class containing the FTP raw text, response code, and other information.
    ''' </summary>
    Public Class FtpResponse
        Private _rawText As String
        Private _text As String
        Private _code As FtpResponseCode = FtpResponseCode.None
        Private _isInformational As Boolean

        ''' <summary>
        ''' Default constructor for FtpResponse.
        ''' </summary>
        Public Sub New()
        End Sub

        ''' <summary>
        ''' Constructor for FtpResponse.
        ''' </summary>
        ''' <param name="rawText">Raw text information sent from the FTP server.</param>
        Public Sub New(rawText As String)
            _rawText = rawText
            _text = ParseText(rawText)
            _code = ParseCode(rawText)
            _isInformational = ParseInformational(rawText)
        End Sub

        ''' <summary>
        ''' Constructor for FtpResponse.
        ''' </summary>
        ''' <param name="response">FtpResponse object.</param>
        Public Sub New(response As FtpResponse)
            _rawText = response.RawText
            _text = response.Text
            _code = response.Code
            _isInformational = response.IsInformational
        End Sub

        ''' <summary>
        ''' Get raw server response text information.
        ''' </summary>
        Public ReadOnly Property RawText() As String
            Get
                Return _rawText
            End Get
        End Property

        ''' <summary>
        ''' Get the server response text.
        ''' </summary>
        Public ReadOnly Property Text() As String
            Get
                Return _text
            End Get
        End Property

        ''' <summary>
        ''' Get a value indicating the FTP server response code.
        ''' </summary>
        Public ReadOnly Property Code() As FtpResponseCode
            Get
                Return _code
            End Get
        End Property

        Friend ReadOnly Property IsInformational() As Boolean
            Get
                Return _isInformational
            End Get
        End Property

        Private Function ParseCode(rawText As String) As FtpResponseCode
            Dim code As FtpResponseCode = FtpResponseCode.None

            If rawText.Length >= 3 Then
                Dim codeString As String = rawText.Substring(0, 3)
                Dim codeInt As Integer = 0

                If Int32.TryParse(codeString, codeInt) Then
                    code = DirectCast(codeInt, FtpResponseCode)
                End If
            End If

            Return code
        End Function

        Private Function ParseText(rawText As String) As String
            If rawText.Length > 4 Then
                Return rawText.Substring(4).Trim()
            Else
                Return String.Empty
            End If
        End Function

        Private Function ParseInformational(rawText As String) As Boolean
            If rawText.Length >= 4 AndAlso rawText(3) = "-"c Then
                Return True
            Else
                Return False
            End If
        End Function


    End Class
End Namespace
